package com.nine.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nine.common.entity.OrderForm;
import com.nine.config.AppContextUtil;
import com.nine.system.mapper.OrderMapper;
import com.nine.system.service.OrderService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MyJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // 在这里编写你的任务逻辑

        OrderService orderService = (OrderService) AppContextUtil.getBean(OrderService.class);
        OrderMapper orderMapper = (OrderMapper) AppContextUtil.getBean(OrderMapper.class);
        LambdaQueryWrapper<OrderForm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderForm::getOrderStatus,2);
        List<OrderForm> orderForms = orderMapper.selectList(wrapper);

        for(int i  = 0;i<orderForms.size();i++){
            OrderForm orderForm = orderForms.get(i);

            LocalDateTime quartzTime = orderForm.getQuartzTime();
            if(Duration.between(quartzTime,LocalDateTime.now()).toMinutes()> 1 ){
                orderService.orderFinished(orderForm.getOrderId(),3);

                System.out.println("自动提交订单 ID: "+orderForm.getOrderId());
            }
        }

    }
}
